import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import "../styles" as Styles

/**
 * CustomDialog - 无边框现代化对话框组件
 * 特性：
 * - 无边框设计，支持拖拽移动
 * - 模态遮罩层，带半透明背景
 * - 自定义标题栏和操作按钮
 * - 统一系统风格
 */
Item {
    id: root

    // 公开属性
    property string title: ""
    property bool modal: true
    property alias contentItem: contentContainer.data
    property int dialogWidth: 500
    property int dialogHeight: 400

    // 按钮配置
    property bool showOkButton: true
    property bool showCancelButton: true
    property string okButtonText: qsTr("确定")
    property string cancelButtonText: qsTr("取消")
    property bool okButtonEnabled: true

    // 信号
    signal accepted()
    signal rejected()
    signal closed()
    signal opened()

    // 可见性控制
    visible: false
    anchors.fill: parent
    z: 1000

    // 模态遮罩层
    Rectangle {
        id: overlay
        anchors.fill: parent
        color: "#80000000"  // 半透明黑色
        opacity: root.visible ? 1 : 0

        Behavior on opacity {
            NumberAnimation {
                duration: Styles.Theme.animationFast
            }
        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                if (root.modal) {
                    // 模态模式下点击遮罩层不关闭
                } else {
                    root.close()
                }
            }
        }
    }

    // 对话框主体
    Rectangle {
        id: dialogBox
        anchors.centerIn: parent
        width: root.dialogWidth
        height: root.dialogHeight
        color: Styles.Theme.surface
        radius: Styles.Theme.radiusLarge
        border.color: Styles.Theme.border
        border.width: 2

        // 缩放动画
        scale: root.visible ? 1 : 0.9
        opacity: root.visible ? 1 : 0

        Behavior on scale {
            NumberAnimation {
                duration: Styles.Theme.animationNormal
                easing.type: Easing.OutCubic
            }
        }

        Behavior on opacity {
            NumberAnimation {
                duration: Styles.Theme.animationNormal
            }
        }

        ColumnLayout {
            anchors.fill: parent
            spacing: 0

            // 标题栏（可拖拽）
            Rectangle {
                id: titleBar
                Layout.fillWidth: true
                Layout.preferredHeight: 60
                color: Styles.Theme.backgroundSecondary
                radius: Styles.Theme.radiusLarge

                Rectangle {
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                    height: parent.radius
                    color: parent.color
                }

                RowLayout {
                    anchors.fill: parent
                    anchors.leftMargin: Styles.Theme.spacingLarge
                    anchors.rightMargin: Styles.Theme.spacingNormal
                    spacing: Styles.Theme.spacingNormal

                    // 标题文本 - 可拖拽区域
                    Item {
                        Layout.fillWidth: true
                        Layout.fillHeight: true

                        Text {
                            anchors.left: parent.left
                            anchors.verticalCenter: parent.verticalCenter
                            text: root.title
                            color: Styles.Theme.textPrimary
                            font.pixelSize: Styles.Theme.fontSizeLarge
                            font.weight: Styles.Theme.fontWeightMedium
                        }

                        // 拖拽处理器 - 只在标题文本区域生效，不影响对话框内的控件
                        DragHandler {
                            id: titleBarDragHandler
                            grabPermissions: TapHandler.CanTakeOverFromAnything
                            target: dialogBox
                        }
                    }

                    // 关闭按钮
                    Rectangle {
                        Layout.preferredWidth: 36
                        Layout.preferredHeight: 36
                        radius: Styles.Theme.radiusSmall
                        color: closeButtonArea.containsMouse ? Styles.Theme.danger : "transparent"

                        Behavior on color {
                            ColorAnimation { duration: Styles.Theme.animationFast }
                        }

                        Text {
                            anchors.centerIn: parent
                            text: "×"
                            color: Styles.Theme.textPrimary
                            font.pixelSize: 24
                            font.weight: Font.Light
                        }

                        MouseArea {
                            id: closeButtonArea
                            anchors.fill: parent
                            hoverEnabled: true
                            cursorShape: Qt.PointingHandCursor
                            onClicked: root.close()
                        }
                    }
                }
            }

            // 内容区域
            Rectangle {
                id: contentArea
                Layout.fillWidth: true
                Layout.fillHeight: true
                color: "transparent"

                ScrollView {
                    anchors.fill: parent
                    anchors.margins: Styles.Theme.spacingLarge
                    clip: true

                    Item {
                        id: contentContainer
                        width: contentArea.width - Styles.Theme.spacingLarge * 2
                        implicitHeight: childrenRect.height
                    }
                }
            }

            // 底部按钮栏
            Rectangle {
                id: buttonBar
                Layout.fillWidth: true
                Layout.preferredHeight: 70
                color: Styles.Theme.backgroundSecondary
                radius: Styles.Theme.radiusLarge
                visible: root.showOkButton || root.showCancelButton

                Rectangle {
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.top: parent.top
                    height: parent.radius
                    color: parent.color
                }

                RowLayout {
                    anchors.fill: parent
                    anchors.margins: Styles.Theme.spacingNormal
                    spacing: Styles.Theme.spacingNormal

                    Item { Layout.fillWidth: true }

                    // 取消按钮
                    Rectangle {
                        visible: root.showCancelButton
                        Layout.preferredWidth: 100
                        Layout.preferredHeight: 40
                        radius: Styles.Theme.radiusSmall
                        color: cancelButtonArea.containsMouse ? Styles.Theme.surfaceVariant : Styles.Theme.surface
                        border.color: Styles.Theme.border
                        border.width: 1

                        Behavior on color {
                            ColorAnimation { duration: Styles.Theme.animationFast }
                        }

                        Text {
                            anchors.centerIn: parent
                            text: root.cancelButtonText
                            color: Styles.Theme.textPrimary
                            font.pixelSize: Styles.Theme.fontSizeNormal
                        }

                        MouseArea {
                            id: cancelButtonArea
                            anchors.fill: parent
                            hoverEnabled: true
                            cursorShape: Qt.PointingHandCursor
                            onClicked: {
                                root.rejected()
                                root.close()
                            }
                        }
                    }

                    // 确定按钮
                    Rectangle {
                        visible: root.showOkButton
                        Layout.preferredWidth: 100
                        Layout.preferredHeight: 40
                        radius: Styles.Theme.radiusSmall
                        color: okButtonArea.containsMouse ? Styles.Theme.primaryDark : Styles.Theme.primary
                        opacity: root.okButtonEnabled ? 1 : 0.5

                        Behavior on color {
                            ColorAnimation { duration: Styles.Theme.animationFast }
                        }

                        Text {
                            anchors.centerIn: parent
                            text: root.okButtonText
                            color: Styles.Theme.textOnPrimary
                            font.pixelSize: Styles.Theme.fontSizeNormal
                            font.weight: Styles.Theme.fontWeightMedium
                        }

                        MouseArea {
                            id: okButtonArea
                            anchors.fill: parent
                            hoverEnabled: true
                            enabled: root.okButtonEnabled
                            cursorShape: Qt.PointingHandCursor
                            onClicked: {
                                root.accepted()
                                root.close()
                            }
                        }
                    }
                }
            }
        }
    }

    // 公开方法
    function open() {
        root.visible = true
        // 重置对话框位置到中心
        dialogBox.x = (root.width - dialogBox.width) / 2
        dialogBox.y = (root.height - dialogBox.height) / 2
        root.opened()
    }

    function close() {
        root.visible = false
        root.closed()
    }
}
